<?php
namespace Hui\FrontEndBundle\Logic;


use Doctrine\ORM\EntityManager;

class SanPhamLogic {
	//connection
	private $connection;
	
	function __construct($connection) {
		$this->connection = $connection;
	}
	
	/**
	 * get all may anh
	 * 
	 * @author anrew
	 */
	public function getAllMayAnh() {
		$em = $this->connection;
		return $em->getRepository('HuiStoreBundle:MayAnh')
						->getAllMayAnh();
	} 
	
	
	/**
	 * get may anh moi
	 * 
	 * @author anrew
	 */
	public function getAllMayAnhMoi() {
		$em = $this->connection;
		return $em->getRepository('HuiStoreBundle:MayAnh')
						->getAllMayAnhMoi();
	}
	
	
	/**
	 * get mai anh duoi 5 trieu
	 * 
	 * @author anrew
	 */
	public function getMayAnhDuoi() {
		$em = $this->connection;
		return $em->getRepository('HuiStoreBundle:MayAnh')
						->getMayAnhDuoi();
	}
	
	/**
	 * get may anh theo Id
	 * 
	 * @author anrew
	 * @param unknown $id
	 */
	public function getMayAnhById($id) {
		$em = $this->connection;
		return $em->getRepository('HuiStoreBundle:MayAnh')
						->getMayAnhById($id);
	}
	
	/**
	 * get all may quay
	 *
	 * @author anrew
	 */
	public function getAllMayQuay() {
		$em = $this->connection;
		return $em->getRepository('HuiStoreBundle:MayQuay')
						->getAllMayQuay();
	}
	
	/**
	 * get all may quay moi
	 * 
	 * @author anrew
	 */
	public function getAllMayQuayMoi() {
		$em = $this->connection;
		return $em->getRepository('HuiStoreBundle:MayQuay')
						->getAllMayQuayMoi();
	}
	
	/**
	 * get may quay duoi 10 trieu
	 * 
	 * @author anrew
	 */
	public function getMayQuayDuoi() {
		$em = $this->connection;
		return $em->getRepository('HuiStoreBundle:MayQuay')
						->getMayQuayDuoi();
	}
	
	/**
	 * get may quay theo Id
	 *
	 * @author anrew
	 * @param unknown $id
	 */
	public function getMayQuayById($id) {
		$em = $this->connection;
		return $em->getRepository('HuiStoreBundle:MayQuay')
						->getMayQuayById($id);
	}
	
	/**
	 * lay top 4 san pham xem nhieu
	 * 
	 * @author anrew
	 * 
	 * @return multitype:
	 */
	public function getSanPhamXemNhieu() {
		$lstSanPhamXemNhieu = array();
		$em = $this->connection;
		$lstTTSP = $em->getRepository('HuiStoreBundle:ThongTinSanPham')
							->getSanPhamXemNhieu();
		foreach ($lstTTSP as $thongTinSanPham) {
			$mayAnh = $em->getRepository('HuiStoreBundle:MayAnh')
								->getMayAnhByTT($thongTinSanPham);
			$mayQuay = $em->getRepository('HuiStoreBundle:MayQuay')
								->getMayQuayByTT($thongTinSanPham);
			if (null != $mayAnh) {
				array_push($lstSanPhamXemNhieu, $mayAnh);
			}
			else if (null != $mayQuay) {
				array_push($lstSanPhamXemNhieu, $mayQuay);
			}
		}
		
		return $lstSanPhamXemNhieu;
	}
	
	/**
	 * danh sach san pham co khuyen mai
	 *
	 * @author anrew
	 *
	 * @return multitype:
	 */
	public function getSanPhamKhuyenMai() {
		$lstSanPhamKhuyenMai = array();
		$em = $this->connection;
		$lstTTSP = $em->getRepository('HuiStoreBundle:ThongTinSanPham')
							->getSanPhamKhuyenMai();
		foreach ($lstTTSP as $thongTinSanPham) {
			$mayAnh = $em->getRepository('HuiStoreBundle:MayAnh')
							->getMayAnhByTT($thongTinSanPham);
			$mayQuay = $em->getRepository('HuiStoreBundle:MayQuay')
							->getMayQuayByTT($thongTinSanPham);
			if (null != $mayAnh) {
				array_push($lstSanPhamKhuyenMai, $mayAnh);
			}
			else if (null != $mayQuay) {
				array_push($lstSanPhamKhuyenMai, $mayQuay);
			}
		}
	
		return $lstSanPhamKhuyenMai;
	}
	
	/**
	 * danh sach san pham co khuyen mai
	 *
	 * @author anrew
	 *
	 * @return multitype:
	 */
	public function getSearchResult($condition) {
		$lstSearchResult = array();
		$em = $this->connection;
		$lstTTSP = $em->getRepository('HuiStoreBundle:ThongTinSanPham')
							->getSearchResult($condition);
		foreach ($lstTTSP as $thongTinSanPham) {
			$mayAnh = $em->getRepository('HuiStoreBundle:MayAnh')
							->getMayAnhByTT($thongTinSanPham);
			$mayQuay = $em->getRepository('HuiStoreBundle:MayQuay')
							->getMayQuayByTT($thongTinSanPham);
			if (null != $mayAnh) {
				array_push($lstSearchResult, $mayAnh);
			}
			else if (null != $mayQuay) {
				array_push($lstSearchResult, $mayQuay);
			}
		}
	
		return $lstSearchResult;
	}
		
	/**
	 * get thongTinSanPhamById
	 *
	 * @author anrew
	 *
	 * @return multitype:
	 */
	public function getThongTinSanPhamById($id) {
		$em = $this->connection;
		return $em->getRepository('HuiStoreBundle:ThongTinSanPham')
						->findById($id);
	}
	
}